package com.example.elmmerchants.Setting;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.NumberPicker;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.Product_Management.Shop_Management_Page;
import com.example.elmmerchants.Product_Management.Update_Product_Data_Page;
import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Set_Packing_Fee_Page extends AppCompatActivity {

    TextView packing_fee_text_view, change_packing_fee, save;

    String email;
    double packing_fee;
    String[] packing_fee_list = {"0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0"};
    String set_how_many_packing_fee;
    boolean is_finish_upload = false;
    AlertDialog dialog;

    private class Update_Data_Thread extends Thread {
        @Override
        public void run() {
            is_finish_upload = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "update `elm`.`merchant` set packing_fee = ? WHERE email = ?";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setDouble(1, Double.parseDouble(set_how_many_packing_fee));
                    preparedStatement.setString(2, email);
                    preparedStatement.executeUpdate();

                    is_finish_upload = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_finish_upload) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                    finish();
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_packing_fee_page);

        packing_fee = getIntent().getDoubleExtra("packing_fee", 0);
        email = getIntent().getStringExtra("email");

        packing_fee_text_view = findViewById(R.id.packing_fee_text_view);
        change_packing_fee = findViewById(R.id.change_packing_fee);
        save = findViewById(R.id.save);

        packing_fee_text_view.setText(packing_fee + "元");
        set_how_many_packing_fee = String.valueOf(packing_fee);

        change_packing_fee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Dialog dialog = new Dialog(view.getContext());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.choose_how_many_packing_fee);

                TextView close = dialog.findViewById(R.id.close);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });

                NumberPicker start_day = dialog.findViewById(R.id.set_packing_fee);
                start_day.setDisplayedValues(packing_fee_list);
                start_day.setMaxValue(packing_fee_list.length - 1);
                start_day.setMinValue(0);
                start_day.setValue((int) (packing_fee * 10));

                TextView save = dialog.findViewById(R.id.save);
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        packing_fee_text_view.setText(packing_fee_list[start_day.getValue()] + "元");
                        set_how_many_packing_fee = packing_fee_list[start_day.getValue()];
                        dialog.dismiss();
                    }
                });

                dialog.show();
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();

                Update_Data_Thread updateDataThread = new Update_Data_Thread();
                updateDataThread.start();
            }
        });
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater1 = LayoutInflater.from(this);
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}